package com.feng.oauth2.oauth.service.impl;


import com.feng.oauth2.oauth.domain.userUrl.UserUrlEnum;
import com.feng.oauth2.oauth.domain.userUrl.UserUrls;
import com.feng.oauth2.oauth.domain.userUrl.UserUrlsRepository;
import com.feng.oauth2.oauth.service.UserUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("userUrlService")
public class UserUrlServiceImpl implements UserUrlService {

    @Autowired
    private UserUrlsRepository urlsRepository;

    @Override
    public UserUrls findUserUrlsById(Integer id) {
        return urlsRepository.findUserUrlsById(id);
    }

    @Override
    public List<UserUrls> findAllUserUrls(String username) {
        return urlsRepository.findAllUserUrls(username);
    }

    @Override
    public void saveUserUrls(UserUrls userUrls) {
        urlsRepository.saveUserUrls(userUrls);
    }

    @Override
    public void updateUserUrls(UserUrls userUrls) {
        urlsRepository.updateUserUrls(userUrls);
    }

    @Override
    public void deleteUserUrls(Integer id) {
        urlsRepository.deleteUserUrls(id);
    }

    @Override
    public void saveAllUrls(UserUrls userUrls) {
        List<UserUrls> urlList = new ArrayList<UserUrls>();
        for (UserUrlEnum url : UserUrlEnum.values()) {
            UserUrls u = new UserUrls();
            u.setUsername(userUrls.getUsername());
            u.setWebUrl(url.url());
            urlList.add(u);
        }
        if(urlList.size()>0){
            urlsRepository.saveAllUrls(urlList);
        }
    }

    @Override
    public void deleteUserAllUrls(String username) {
        urlsRepository.deleteUserAllUrls(username);
    }

    @Override
    public void saveSelfUrls(List<UserUrls> urlList) {
        if(urlList.size()>0){
            urlsRepository.saveAllUrls(urlList);
        }
    }
}